package cn.kgc.tangcco.controller;

import cn.kgc.tangcco.model.LayPage;
import cn.kgc.tangcco.model.PageParam;
import cn.kgc.tangcco.model.ResultCode;
import cn.kgc.tangcco.pojo.Idiom;
import cn.kgc.tangcco.service.IdiomService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/12/29 16:48
 */
@RestController
@RequestMapping(value = "/idiom")
public class IdiomController {
    @Autowired
    private IdiomService idiomService;

    @RequestMapping("/query")
    public LayPage<Idiom> query(@RequestHeader(value = "Authentication", required = false) String token,
                                @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit) {
        if (StringUtils.isBlank(token)) {
            LayPage<Idiom> layPage = new LayPage<>();
            layPage.setCode(Integer.parseInt(ResultCode.NO_LOGIN.getCode()));
            layPage.setMsg(ResultCode.NO_LOGIN.getMsg());
            return layPage;
        }
        return idiomService.query(token, new PageParam(page, limit));
    }
}
